using seanfoy.oopsnet;
using System;
using System.Windows.Forms;

public class WinForm : Form {
    public WinForm() {
        Button b = new Button();
        b.Text = "throw";
        b.Click += new EventHandler(click);
        Controls.Add(b);
    }
    public void click(Object sender, EventArgs args) {
        Raise();
    }
    public void Raise() {
        throw new Exception("error");
    }
    void normalExit(Object sender, EventArgs args) {
        Close();
        Application.Exit();
    }
    public void autopilotDriver(bool crash) {
        EventHandler delayed =
            crash ?
                new EventHandler(click) :
                new EventHandler(normalExit);
        Timer t = new Timer();
        t.Tick += delayed;
        t.Interval = 50; //ms
        t.Start();
    }
    public delegate void AutopilotDriver(bool crash);
    public static void Main(String [] args) {
        using (WinFormsErrorHandler.install()) {
            WinForm f = new WinForm();
            f.Visible = true;
            f.Invoke(
                new AutopilotDriver(f.autopilotDriver),
                new object [] {
                    args.Length > 0 && "crash".Equals(args[0])});
            Application.Run(f);
        }
    }
}
